/**
 * Created by Amaris on 2016/9/13.
 */
var agent = require('superagent');
var utils = require("../utils/util.js");
var conf = require('config');

exports.httpSendMethod = {

    //POST请求方法
    post: function (postUrl, params, cb) {
        var url = conf.get("api_request_url") + postUrl;
        agent
            .post(url)
            .set('Content-Type', 'application/x-www-form-urlencoded')  // !important
            .send(params.body)
            .end(function (err, response) {
                try {
                    cb(err, response.text);
                } catch (err) {
                    var errorMsg
                            = '\n'
                            + 'Error ' + new Date().toISOString()
                            + '\n'
                            + err.stack || err.message || 'unknow error'
                            + '\n'
                        ;
                    cb(err, errorMsg);
                    console.error(errorMsg);
                }
            });
    },

    //GET请求方法
    get: function (getUrl, params, cb) {
        var url = conf.get("api_request_url") + getUrl;
        agent
            .get(url)
            .query(params.query)
            .end(function (err, response) {
                try {
                    cb(err, response.text);
                } catch (err) {
                    var errorMsg
                            = '\n'
                            + 'Error ' + new Date().toISOString()
                            + '\n'
                            + err.stack || err.message || 'unknow error'
                            + '\n'
                        ;
                    cb(err, errorMsg);
                    console.error(errorMsg);
                }
            });
    }
};